package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.model.OrderModel;
import com.armani.model.Result;
import com.armani.service.OrderService;
import com.armani.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: OrderController
 * @Description: 订单详情控制层（url）
 * @Author: lenovo
 * @Date: 2021/11/29 13:41
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/order/", produces = "text/html;charset=UTF-8")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询订单列表展示
     * @param orderNum 订单编号
     * @param userId 用户id
     * @param payWay 支付方式
     * @param orderTime 下单时间
     * @param page 页数
     * @param limit 一页展示的条数
     * @return 订单列表展示集合
     */
    @GetMapping(value = "detail.do")
    @ResponseBody
    public String detail(@RequestParam(required = false) String orderNum,
                         @RequestParam(required = false) Integer userId,
                         @RequestParam(required = false) Integer payWay,
                         @RequestParam(required = false) String orderTime,
                         @RequestParam(required = false) Integer page,
                         @RequestParam(required = false) Integer limit){
        //参数列表
        Map<String,Object> params=new HashMap<>();
        params.put("orderNum", orderNum);
        params.put("userId", userId);
        params.put("payWay", payWay);
        params.put("orderTime", orderTime);
        if (page!=null && limit !=null){
            params.put("offset",(page-1)*limit);
            params.put("limit",limit);
        }
        List<OrderModel> orderList=orderService.detail(params);
        Integer count=orderService.count(params);
        Result result=new Result();
        result.setCode(0);
        result.setData(orderList);
        result.setCount(count);
        result.setMsg("查询成功");
        return JsonUtil.stringify(result);
    }


    @PostMapping(value = "del.do")
    @ResponseBody
    @Transactional
    public String del(@RequestParam Integer userId) throws Exception {
        Integer row = orderService.del(userId);
        Result result = new Result();
        if (row == 0) {
            result.setCode(1000);
            result.setMsg("删除失败！");
        } else {
            result.setCode(0);
            result.setMsg("删除成功！");
        }
        String json = JSON.toJSONString(result);
        return json;
    }
}
